package com.web.admin.web.cms.pan;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.http.message.ViewBuilder;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.join.UserJoinHandler;
import com.web.base.user.facade.UserFacade;
import com.web.admin.global.model.User;
import com.web.admin.global.util.HttpContext;
import com.web.cms.pan.exception.ShareException;
import com.web.cms.pan.facade.ShareFacade;
import com.web.cms.pan.message.ShareLogRequest;
import com.web.cms.pan.message.ShareLogResponse;
import com.web.cms.pan.message.ShareRequest;
import com.web.cms.pan.message.ShareResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * @author zhengjin
 * @since 1.0 2018年05月09日
 */
@Controller
@Http(url = "/cms/pan")
public class ShareController
{
	@Autowired
	private ShareFacade shareFacade;
	@Autowired
	private UserFacade userFacade;
	@Resource
	private HttpContext httpContext;

	private DictParser dictParser = DictParser.getInstance();

	public ShareController()
	{
	}

	@Http(url = "/share_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		ShareRequest shareRequest = request.getQueryObject(ShareRequest.class);
		shareRequest.setUserId(user.getId());
		Page<ShareResponse> page = shareFacade.listPage(shareRequest, Const.PAGE_START, Const.PAGE_SIZE);
		return View.createBuilder().setView("/cms/pan/share_list")
						.put("map", dictParser.getStringMap())
						.put("model", shareRequest)
						.put("page", page)
						.build();
	}

	@Http(url = "/share_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		ShareRequest shareRequest = request.getPostObject(ShareRequest.class);
		int start = request.getPostInt("start", 1);
		shareRequest.setUserId(user.getId());
		Page<ShareResponse> page = shareFacade.listPage(shareRequest, start, Const.PAGE_SIZE);
		return View.createBuilder().setView("/cms/pan/share_list_grid")
						.put("page", page)
						.build();
	}

	@Http(url = "/share_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		ShareRequest shareRequest = request.getPostObject(ShareRequest.class);
		shareRequest.setUserId(user.getId());
		if (shareRequest.getId() == null)
		{
			ValidationUtil.valid(shareRequest, SaveModel.class);
			shareFacade.add(shareRequest);
		}
		else
		{
			ValidationUtil.valid(shareRequest, UpdateModel.class);
			shareFacade.update(shareRequest);
		}
	}

	@Http(url = "/share_delete", method = HttpMethod.POST)
	public void delete(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		String ids = request.getPost("ids");
		shareFacade.delete(user.getId(), ids);
	}

	@Http(url = "/share_get_code", method = HttpMethod.GET)
	public View getCode(HttpRequest request)
	{
		return View.createBuilder().setView("/cms/pan/share_code")
						.put("map", dictParser.getStringMap())
						.build();
	}

	@Http(url = "/share_code", method = HttpMethod.GET)
	public View code(HttpRequest request)
	{
		ViewBuilder builder = View.createBuilder();
		String code = request.getQueryString("code");
		String shareCode = httpContext.getShareCode(request);
		if (shareCode == null || !shareCode.equals(code))
			return builder.setLocation("/cms/pan/share_get_code").build();

		User user = httpContext.getUser(request);
		ShareResponse shareResponse = shareFacade.getByCode(user.getId(), code);

		return builder.build();
	}

	@Http(url = "/share_valid", method = HttpMethod.POST)
	public Object valid(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		ShareRequest shareRequest = request.getPostObject(ShareRequest.class);
		ShareResponse shareResponse = shareFacade.valid(user.getId(), shareRequest.getCode(), shareRequest.getPassword());
		if (shareResponse == null)
			throw new ShareException("分享验证失败");

		httpContext.setShareCode(request, shareResponse.getCode());
		return shareResponse;
	}

	@Http(url = "/share_log_list", method = HttpMethod.GET)
	public View listLogGet(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		ShareLogRequest shareLogRequest = request.getQueryObject(ShareLogRequest.class);
		ShareResponse shareResponse = shareFacade.get(user.getId(), shareLogRequest.getShareId());
		Page<ShareLogResponse> page = shareFacade.listLogPage(shareLogRequest, Const.PAGE_START, Const.PAGE_SIZE);
		UserJoinHandler.userJoin(page.getObjectList(), userFacade);
		return View.createBuilder().setView("/cms/pan/share_log_list")
						.put("map", dictParser.getStringMap())
						.put("model", shareResponse)
						.put("page", page)
						.build();
	}

	@Http(url = "/share_log_list", method = HttpMethod.POST)
	public View listLogPost(HttpRequest request)
	{
		ShareLogRequest shareLogRequest = request.getPostObject(ShareLogRequest.class);
		int start = request.getPostInt("start", 1);
		Page<ShareLogResponse> page = shareFacade.listLogPage(shareLogRequest, start, Const.PAGE_SIZE);
		UserJoinHandler.userJoin(page.getObjectList(), userFacade);
		return View.createBuilder().setView("/cms/pan/share_log_list_grid")
						.put("page", page)
						.build();
	}

}
